import dayjs from "dayjs";
import { getUserPage, deleteUser } from "@/api/sys/user";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, computed, onMounted } from "vue";

export function useUser() {
  const userFormRef = ref();
  const editFormRef = ref();
  const bindOrgFormRef = ref();
  const bindRoleFormRef = ref();
  const form = reactive({
    username: "",
    orgId: "",
    pageNum: 1,
    pageSize: 10
  });
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      width: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "用户账户",
      prop: "username",
      minWidth: 130
    },
    {
      label: "用户昵称",
      prop: "extend.nickname",
      minWidth: 130
    },
    {
      label: "部门",
      prop: "orgName",
      minWidth: 90,
      formatter: ({ orgName, orgId }) => {
        if (orgId == "2") {
          return "未绑定";
        } else {
          return orgName;
        }
      }
    },
    {
      label: "邮箱",
      prop: "extend.email",
      minWidth: 90
    },
    {
      label: "是否在线",
      prop: "isOnline",
      minWidth: 90,
      cellRenderer: ({ isOnline }) => (
        <el-tag type={isOnline === 1 ? "danger" : ""} effect="plain">
          {isOnline === 1 ? "在线" : "离线"}
        </el-tag>
      )
    },
    {
      label: "创建时间",
      minWidth: 90,
      prop: "updatedAt",
      formatter: ({ updatedAt }) => {
        return dayjs(Number(updatedAt) * 1000).format("YYYY-MM-DD HH:mm:ss");
      }
    },
    {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ];
  const buttonClass = computed(() => {
    return [
      "!h-[20px]",
      "reset-margin",
      "!text-gray-500",
      "dark:!text-white",
      "dark:hover:!text-primary"
    ];
  });

  function handleUpdate(row: any) {
    editFormRef.value.showEditDialog(row);
  }

  function handleDelete(row) {
    deleteUser(row.userId).then(() => {
      onSearch();
    });
  }

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }

  async function onSearch() {
    loading.value = true;
    const { list, total } = await getUserPage(form);
    dataList.value = list;
    pagination.total = total;
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  onMounted(() => {
    onSearch();
  });

  return {
    userFormRef,
    editFormRef,
    bindOrgFormRef,
    bindRoleFormRef,
    form,
    loading,
    columns,
    dataList,
    pagination,
    buttonClass,
    onSearch,
    resetForm,
    handleUpdate,
    handleDelete,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
